# coding=utf-8
import os
import pandas as pd
import math
from settings  import DOWN_FILE_DIR
                             
def split_file(filename, col_names):
    # 读取文件的大小，单位为B
    file_path = '{}{}'.format(DOWN_FILE_DIR,filename)
    files_=[file_path]
    size = os.path.getsize(file_path)
    # 转化单位为MB
    mb_size = float(size)/(1024*1024) 
    # 拆分文件的次数，以3MB为每个文件的大小
    if mb_size<=3:
        return files_
    file_num = math.ceil(mb_size/3)      
    # 获得每个文件需要有的行数
    data = pd.read_excel(filename)
    num = len(data)
    chunksize = round(num / file_num + 1)
    time_ = 0  # 定文件名
    while time_<file_num:
        data_t = data.iloc[time_*chunksize:chunksize*(time_+1),:]
        file_path='{}{}{}.xlsx'.format(DOWN_FILE_DIR,filename.spilt('-'),time_)
        time_ +=1
        data_t.to_excel(file_path,columns=col_names,index=False)
        files_.append(files_)
    return files_

